package com.fita.admin.service.report.impl;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.fita.admin.dao.ReportDao;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.report.ReportService;
import com.fita.entity.order.Order;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 
 * @author sjh
 *
 */

@Service("reportService")
public class ReportServiceImpl extends DataServiceImpl<Order> implements ReportService{
	
	@Dao
	private ReportDao reportDao;
	
	
	/*
	 * (non-Javadoc)
	 * @see com.fita.admin.service.report.ReportService#count()
	 */
	public int count() {
		
		return reportDao.count();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.fita.admin.service.report.ReportService#list()
	 */
	public List<Order> list(Page page) {
			
		List<Order> list = reportDao.list(page.getBeginIndex(), page.getEveryPage());
			
			page.calculate();
			
			return list;
	}

    /* (non-Javadoc)
     * @see com.fita.admin.service.report.ReportService#calculate(java.util.List)
     */
    public Map<String, Object> calculate(List<Order> orders) {

        Map<String, Object> results = new HashMap<String, Object>();
        
        if (CollectionUtils.isEmpty(orders)) {
            return results;
        }
        BigDecimal totalCost = new BigDecimal(0);
        BigDecimal totalSelling  = new BigDecimal(0);
        BigDecimal totalGp = new BigDecimal(0);
        BigDecimal totalMargin = new BigDecimal(0);

        for (Order order : orders) {
            
            Map<String,Object> datas = new HashMap<String, Object>();
            
            BigDecimal gp = order.getOrderActualPayPrice().subtract(order.getOrderCostPrice());
            
            datas.put("gp", gp);
            
            if (order.getOrderActualPayPrice().compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal margin = gp.divide(order.getOrderActualPayPrice(), RoundingMode.HALF_UP);
                datas.put("margin", margin);
            }else {
                datas.put("margin", new BigDecimal(0));
            }
            results.put(order.getId().toString(), datas);
            
            totalSelling = totalSelling.add(order.getOrderActualPayPrice());
            
            totalCost = totalCost.add(order.getOrderCostPrice());
            
            totalGp = totalGp.add(gp);
        }
        
        if (totalSelling.compareTo(BigDecimal.ZERO) > 0) {
            totalMargin = totalGp.divide(totalSelling, RoundingMode.HALF_UP);
        }else {
            totalMargin = new BigDecimal(0);
        }
        Map<String, Object> total = new HashMap<String, Object>();
        
        total.put("totalCost", totalCost);
        
        total.put("totalSelling", totalSelling);
        
        total.put("totalGp", totalGp);
        
        total.put("totalMargin", totalMargin);
        
        results.put("total", total);
        
        return results;
    }


	

}
